﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using com.crawler.common;
using com.crawler.components;
using System.Text.RegularExpressions;

namespace com.crawler.winfrom
{
    public partial class FrmBookSite : Form
    {
        private List<BookSiteInfo> m_book_site_list = null;

        public FrmBookSite()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_add_Click(object sender, EventArgs e)
        {
            FrmSiteManage t_frm = new FrmSiteManage();
            t_frm.BookSiteList = m_book_site_list;

            DialogResult t_dlr= t_frm.ShowDialog();
           
            if (t_dlr == DialogResult.OK)
            {
                BindList();
            }
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmBookSite_Load(object sender, EventArgs e)
        {
            BindList();
        }

        /// <summary>
        /// 绑定列表
        /// </summary>
        private void BindList()
        {
            try
            {
                m_book_site_list = SerializationHelper.Load(typeof(List<BookSiteInfo>), Consts.SiteFileName) as List<BookSiteInfo>;

                lst_site.Items.Clear();
                if (m_book_site_list != null && m_book_site_list.Count>0)
                {
                    m_book_site_list.Sort(delegate(BookSiteInfo t_a, BookSiteInfo t_b)
                    {
                        if (t_a.SiteId < t_b.SiteId)
                            return -1;
                        if (t_a.SiteId > t_b.SiteId)
                            return 1;
                        return 0;
                       
                    });

                    foreach (BookSiteInfo t_site in m_book_site_list)
                    {
                        string[] t_sub_items = { t_site.SiteId.ToString(), t_site.SiteName, t_site.BookListUrl, t_site.IsForbid ? "禁用" : "正常",string.IsNullOrEmpty( t_site.Memo)?"正常":t_site.Memo };
                        lst_site.Items.Add(new ListViewItem(t_sub_items));

                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定站点列表失败" + ex.ToString());
                MessageBox.Show("绑定站点列表失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_edit_Click(object sender, EventArgs e)
        {
            EditSite();
        }

        /// <summary>
        /// 修改
        /// </summary>
        private void EditSite()
        {
            if (lst_site.SelectedItems.Count == 0)
                return;

            int t_site_id = 0;
            if (!int.TryParse(lst_site.SelectedItems[0].SubItems[0].Text.ToString(), out t_site_id))
                t_site_id = 0;

            if (t_site_id == 0)
            {
                MessageBox.Show("无效的选择项!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            FrmSiteManage t_frm = new FrmSiteManage();

            t_frm.SiteId = t_site_id;
            t_frm.BookSiteList = m_book_site_list;

            DialogResult t_dlr= t_frm.ShowDialog();

            if (t_dlr == DialogResult.OK)
            {
                BindList();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        private void Delete()
        {
            if (lst_site.SelectedItems.Count == 0)
                return;

            int t_site_id = 0;
            if (!int.TryParse(lst_site.SelectedItems[0].SubItems[0].Text.ToString(), out t_site_id))
                t_site_id = 0;

            List<BookSiteInfo> t_list = m_book_site_list.FindAll(delegate(BookSiteInfo t_site)
            {
                return t_site.SiteId == t_site_id;
            });

            if (t_list != null)
            {
                m_book_site_list.Remove(t_list[0]);
            }

            try
            {
               bool t_ret= SerializationHelper.Save(m_book_site_list, Consts.SiteFileName);

               if (t_ret)
               {
                   BindList();
               }
               else
               {
                   MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
               }
            }
            catch (Exception ex)
            {
                LogHelper.Error("保存站点配置失败" + ex.ToString());
                MessageBox.Show("保存站点配置失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_delete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsb_edit_Click(object sender, EventArgs e)
        {
            EditSite();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsb_add_Click(object sender, EventArgs e)
        {
            FrmSiteManage t_frm = new FrmSiteManage();
            t_frm.BookSiteList = m_book_site_list;

            DialogResult t_dlr= t_frm.ShowDialog();
          
            if (t_dlr == DialogResult.OK)
            {
                BindList();
                //InitSiteInDB();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsb_delete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        /// <summary>
        /// 初始化到数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsp_db_Click(object sender, EventArgs e)
        {
            InitSiteInDB();
        }

        /// <summary>
        /// 初始化到数据库
        /// </summary>
        private void InitSiteInDB()
        {
            if (m_book_site_list == null || m_book_site_list.Count == 0)
            {
                return;
            }

            foreach (BookSiteInfo t_site in m_book_site_list)
            {
                Uri t_url = new Uri(t_site.BookListUrl);

                Match t_mc = Regex.Match(t_url.Host.ToString(), "\\S+\\.(\\S+)\\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                if (t_mc != null && t_mc.Groups.Count > 0)
                {
                    t_site.TbName = "tb" + t_mc.Groups[1].Value.ToString();
                }
            }

            try
            {
                DBManager t_db_mgr = new DBManager();
                int t_ret = t_db_mgr.InitBookSite(m_book_site_list);

                if (t_ret > 0)
                {
                    MessageBox.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("操作失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("初始化数据到数据库失败" + ex.ToString());
            }
        }
     
       
    }
}
